<?php	include_once('inc/header.php');
		include_once('inc/product.inc.php');	
	
if(isset($_GET['pr']) && check_product($db, htmlspecialchars($_GET['pr'])))
{ 
	// Initial statements
	$show_product = true;
	$ongoingChanges = false;
	$modifExecuted = false;
	$withdrawAsked = false;
	$makeAvailable = false;
	$pr_id = check_product($db, htmlspecialchars($_GET['pr']));
	
	if (isset($_POST['action']))
	{
		// If a modification has just been executed, we have to update the product informations
		if($_POST['action'] == "productModified")
		{
			$modifExecuted = true;
			
			$req = $db->prepare('UPDATE jktrade_products 
								SET pr_name = :pr_name, pr_price = :pr_price, pr_cat = :pr_cat, pr_shortdesc = :pr_shortdesc, pr_desc = :pr_desc 
								WHERE pr_id = :pr_id');
			$req->bindParam('pr_name', $_POST['pr_name'], PDO::PARAM_STR);
			$req->bindParam('pr_price', $_POST['pr_price'], PDO::PARAM_INT);
			$req->bindParam('pr_cat', $_POST['pr_cat'], PDO::PARAM_INT);
			$req->bindParam('pr_shortdesc', $_POST['pr_shortdesc'], PDO::PARAM_STR);
			$req->bindParam('pr_desc', $_POST['pr_desc'], PDO::PARAM_STR);
			$req->bindParam('pr_id', $pr_id, PDO::PARAM_INT);
			
			$req->execute();
		}
		
		// The user has added the current product to his cart
		if($_POST['action'] == "addToCart")
		{
			actionCart("addToCart", $pr_id);
		}
		

		
		if(!isset($_POST['pr_cat']) OR $_POST['pr_cat']==0){$req = $db->prepare('UPDATE jktrade_products 
							SET pr_name = :pr_name, pr_price = :pr_price, pr_shortdesc = :pr_shortdesc, pr_desc = :pr_desc 
							WHERE pr_id = :pr_id');}
		else{
		$req = $db->prepare('UPDATE jktrade_products 
							SET pr_name = :pr_name, pr_price = :pr_price, pr_cat = :pr_cat, pr_shortdesc = :pr_shortdesc, pr_desc = :pr_desc 
							WHERE pr_id = :pr_id');
		}
		$req->bindParam('pr_name', $_POST['pr_name'], PDO::PARAM_STR);
		$req->bindParam('pr_price', $_POST['pr_price'], PDO::PARAM_INT);
		if(isset($_POST['pr_cat']) AND $_POST['pr_cat']!=0){$req->bindParam('pr_cat', $_POST['pr_cat'], PDO::PARAM_INT);}
		$req->bindParam('pr_shortdesc', $_POST['pr_shortdesc'], PDO::PARAM_STR);
		$req->bindParam('pr_desc', $_POST['pr_desc'], PDO::PARAM_STR);
		$req->bindParam('pr_id', $pr_id, PDO::PARAM_INT);

		// If a modification of his product's informations has been required by the seller
		if($_POST['action'] == "ongoingChanges")
		{
			$ongoingChanges = true;
		}

		
		if($_POST['action'] == "withdrawAsked")
		{
			$withdrawAsked = true;
			
			$req = $db->prepare('UPDATE jktrade_products 
								SET pr_display = 0
								WHERE pr_id = :pr_id');
			$req->bindParam('pr_id', $pr_id, PDO::PARAM_INT);
			
			$req->execute();
		}
		else if($_POST['action'] == "makeAvailable")
		{
			$makeAvailable = true;
			
			$req = $db->prepare('UPDATE jktrade_products 
								SET pr_display = 1
								WHERE pr_id = :pr_id');
			$req->bindParam('pr_id', $pr_id, PDO::PARAM_INT);
			
			$req->execute();
		}
	}
	
	$display = checkDisplay($db, $pr_id);
	$product = new Product($db, $pr_id);
	$seller = new User($db, $product->sellerID);
	$catName = getCatName($db, $product->cat);
	if($connected)
	{
		$isSeller = isSeller ($db, $pr_id, $user);
	}
	else $isSeller = false;
	
	if(isset($ongoingChanges)){ // We include the the form that will host the modifications
			echo'<form novalidate="novalidate" enctype="multipart/form-data" method="post" action="show_product.php?pr='.$product->id.'"';
	} 
	
	// We display the product, only if it is available or if the user is the seller or an administrator 
	if($display || $isSeller)
	{
		echo '
		<!--page-->
		<div id="banner" style="background-image: url(\'img/headerBg.jpg\'); background-color: black; color:white; background-position: center; background-repeat: no-repeat;">
			<div class="container intro_wrapper">
				<div class="inner_content"></div>
					<div class="col-md-12">';
					
					if($ongoingChanges){
						displaySelectCategories($db);
					} 
					else echo '<h3 style="text-transform: uppercase">'.$catName.'</h3>';
					
					if($ongoingChanges){
						echo	'<h1><input name="pr_name" placeholder="Product name" value="'.$product->name.'" type="text"/></h1>';
					} 
					else echo '<h1 style="font-size: 3em; text-transform: uppercase; font-weight: 300; margin: 0 0 0 0; padding: 0 0 0 0;">'.$product->name.'</h1>';
					
					echo'
					</div>
				</div>
			</div>';

	
		
		echo'
		<div class="container wrapper">
			<div class="inner_content">
				<div class="row"> 
					
				<div class="col-md-8 pad25">
				<!-- slider starts -->
             		<div id="nslider" class="nivoSlider"> 
					
					
					<!-- add your images here -->
						<img src="img/uploads/products/large/'.$product->mainpic.'" alt="'.$product->mainpic.'" title="#mainpic" />
						';
						$i=1;
						$req = $db->query('SELECT img_id FROM jktrade_img WHERE img_rel='.$product->id.'');
						while ($data = $req->fetch())
						{	
						$img=new Img($db, $data['img_id']);
						echo'
						<img src="img/uploads/products/large/'.$img->name.'" alt="" title="#nivocaption'.$i.'"/>
						';
						$i++;
						}
						
						echo'
						
                     </div> 
                     <!-- add your captions here -->
						<div id="mainpic" class="nivo-html-caption">'.$product->name.'</div>
 
					</div>
					
					<div class="col-md-4">
                    <h1 style="font-size: 2em; font-weight: 400; color: #314f72; padding: 0 0 0 0;">'.$product->name.'</h1>
				
					<div class="alert alert-info">
						<h3 style="text-transform: uppercase; margin-top: 0px;">PRICE</h3>';
						if($ongoingChanges){
							echo '<input style="font-size: 4em; font-weight: 300; text-align: right;" name="pr_price" id="pr_price" placeholder="Selling price (KR)" value="'.$product->price.'"type="number"/>';
						} 
						else echo '
						<span style="font-size: 4em; font-weight: 300; text-align: right;">'.$product->price.' KR.</span>';
					echo'
					</div>
				
                      <i class="fa fa-quote-left" style="margin-right: 10px;"></i>';
						if($ongoingChanges){
							echo '<textarea name="pr_shortdesc" maxlength="200" style="min-height:120px">'.$product->shortdesc.'</textarea>';
						} 
						else echo '
					  <span style="font-size: 1.5em; font-weight: 400;">'.$product->shortdesc.'</span>';
					  echo'
					  <i class="fa fa-quote-right" style="margin-left: 10px;"></i> 

						<p class="lead">
						</p>';
					
						if($modifExecuted)
						{
							echo '<div class="alert alert-success">
									  <strong>Product\'s informations</strong> have been successfully updated
								</div>';
						}
						else if($withdrawAsked)
						{
							echo '<div class="alert alert-success">
									  <strong>Your product</strong> has been successfully <strong>withdrawn</strong> from sale\'s platform
								</div>';
						}
						else if($makeAvailable)
						{
							echo '<div class="alert alert-success">
									  <strong>Your product</strong> has been successfully <strong>put back</strong> on the sale\'s platform
								</div>';
						}
						
						echo'					
						 <div class="pad20"></div>';

						// If the user is the product's seller he can change the presentation plug
						
						if($ongoingChanges)
						{
							echo '<input style="font-size:1.1em" type="submit" value="Submit the changes" class="btn btn-info btn-danger btn-custom">';
						}
						else if($connected && $isSeller)
						{
							if($display) // If the product is available for all the users
							{
								echo '<form action="show_product.php?pr='.$product->id.'" method="post">
									<input type="hidden" name="action" value="withdrawAsked">
									<input type="submit" style="font-size:1.1em" class="btn btn-info btn-danger btn-custom" value="Withdraw the product from sale">
								</form>';
							}
							else // If the product has been withdrawn from the sell platform
							{
								echo '<form action="show_product.php?pr='.$product->id.'" method="post">
									<input type="hidden" name="action" value="makeAvailable">
									<input type="submit" style="font-size:1.1em" class="btn btn-info btn-warning btn-custom" value="Make available the product for sale">
								</form>';
							}
							
							echo'
							<div class="pad5"></div>
						
							<form action="show_product.php?pr='.$product->id.'" method="post">
								<input type="hidden" name="action" value="ongoingChanges">
								<input type="submit" style="font-size:1.1em" class="btn btn-info btn-primary btn-custom" value="Change your product\'s plug">
							</form>';
						}
						else if ($connected)
						{
							echo '
							<form action="show_product.php?pr='.$product->id.'" method="post">
								<input type="hidden" name="action" value="addToCart">
								<input type="submit" style="font-size:1.1em" class="btn btn-info btn-primary btn-custom" value="Add to cart">
							</form>';
						}
						
						echo '
						<div class="pad30"></div>
					</div>
				</div>
			</div>
		</div>
		
		<div style="background-color: white; background-position: center; background-repeat: no-repeat;">
		<div class="container intro_wrapper">
			<div class="inner_content"></div>
				<div class="col-md-8">';
				if($ongoingChanges){
					echo '<textarea name="pr_desc" style="min-height: 200px;" maxlength="5000" id="pr_desc">'.$product->desc.'</textarea>';
				}
				else echo '
				<h3 style="font-size: 2.3em; text-transform: uppercase; font-weight: 300; padding: 0 0 0 0; margin-bottom: 20px;">About the product</h3>
				'.$product->desc.'';
				echo'
				</div>
				
				<div class="col-md-4" style="background-color: #f0f0f0;">
				<h3 style="font-size: 2.3em; text-transform: uppercase; font-weight: 300; padding: 0 0 0 0; margin-bottom: 20px;">About the seller</h3>
				<div class="col-md-4">
				<img src="img/avatar-default.gif" class="img-rounded" />
				</div>
				<div class="col-md-8">
				<label style="font-size: 1.5em; font-weight: 400;">'.$seller->getName().' ('.$seller->getAge().')</label><br/>
				<span style="text-transform: uppercase;"><span class="label label-success">Verified</span> <a href="account.php?page=inbox&op=sendPIM&user='.$seller->id.'"><span class="label label-primary"><i class="fa fa-envelope"></i> send PIM</span></a><br/></span>
				<label>Member since: <br/> '.$seller->getRegdate().'</label><br/><br/>
				
				</div>
				<hr/>
				
				<h3 style="font-size: 2.3em; text-transform: uppercase; font-weight: 300; padding: 0 0 0 0; margin-bottom: 20px;">Delivery area</h3>
				
				<div id="map">
						<iframe
						  width="100%"
						  height="400px"
						  frameborder="0" style="border:0"
						  src="https://www.google.com/maps/embed/v1/place?key=AIzaSyB0qujj1vZpFojRIwf3BefNyIYZq5FBhE8
							&q=Jonkoping+'.$product->area.'">
						</iframe>
						</div>
				</div>
					
				</div>
			</div>';
			
		if($ongoingChanges){
			echo'<input type="hidden" name="action" value="productModified" /></form>';
		}
		
		echo'		
		<!--related projects-->	
		<div class="strip2">
			<h1>Related Products</h1>
			<h3 class="center about_strip">
				Here is a <span class="hue">selection</span> of products you might <span>like</span>. This is based on the specifications of the <span>above</span> product. 
			</h3>
		
			<div class="container wrapper">
			<div class="inner_content col_full" >
			
			<div id="slider_related">';
			
			displayRandomProducts($db, $product->cat);
			
		echo'
			</div>
				<div id="sl-prev" class="widget-scroll-prev2"><i class="fa fa-chevron-left white"></i></div>
				<div id="sl-next" class="widget-scroll-next2"><i class="fa fa-chevron-right white but_marg"></i></div>
				</div>
			<!--//projects-->
		</div>
	</div>
	</div>';
	}
	
	// If the user is not the seller/admin and the product is not available for public display
	else 
	{	echo '
		<!--page-->
		<div id="banner" style="background-image: url(\'img/headerBg.jpg\'); background-color: black; color:white; background-position: center; background-repeat: no-repeat;">
			<div class="container intro_wrapper">
				<div class="inner_content"></div>
					<div class="col-md-12">
					
					<h1 style="font-size: 3em; text-transform: uppercase; font-weight: 300; margin: 0 0 0 0; padding: 0 0 0 0;">Unavailable product</h1>
				
					<h3 style="text-transform: uppercase">This product is not available yet.</br> You can select another one from the <a href="browse_products.php">product\'s browser!</a></h3>
					
					</div>
				</div>
			</div>';
	}
}
	
// No product has been selected or it doesn\'t correspond to any product in the database
else 
	{	echo '
		<!--page-->
		<div id="banner" style="background-image: url(\'img/headerBg.jpg\'); background-color: black; color:white; background-position: center; background-repeat: no-repeat;">
			<div class="container intro_wrapper">
				<div class="inner_content"></div>
					<div class="col-md-12">
					
					<h1 style="font-size: 3em; text-transform: uppercase; font-weight: 300; margin: 0 0 0 0; padding: 0 0 0 0;">No product found</h1>
				
					<h3 style="text-transform: uppercase">The product may have been deleted.</br> please retry from the <a href="browse_products.php">product\'s browser!</a></h3>
					
					</div>
				</div>
			</div>';
	}
?>


	<!--//page-->
    
<?php	include_once('inc/footer.php'); ?>